package com.atguigu.gmall.controller;

import com.atguigu.gmall.entity.BaseSaleProperty;
import com.atguigu.gmall.entity.ProductImage;
import com.atguigu.gmall.entity.ProductSalePropertyKey;
import com.atguigu.gmall.entity.ProductSpu;
import com.atguigu.gmall.result.RetVal;
import com.atguigu.gmall.service.ImageService;
import com.atguigu.gmall.service.SalePropertyKeyService;
import com.atguigu.gmall.service.SalePropertyService;
import com.atguigu.gmall.service.SpuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class SpuController {

    @Autowired
    private SpuService spuService;
    @Autowired
    private SalePropertyService salePropertyService;
    @Autowired
    private SalePropertyKeyService salePropertyKeyService;
    @Autowired
    private ImageService imageService;

    @GetMapping("queryProductSpuByPage/{currentPage}/{limit}/{category3Id}")
    public RetVal queryProductSpuByPage(@PathVariable Long currentPage,
                                        @PathVariable Long limit,
                                        @PathVariable Long category3Id) {
        IPage<ProductSpu> productSpuPage = new Page<ProductSpu>(currentPage, limit);
        IPage page = spuService.selectPageByCategory3Id(productSpuPage, category3Id);
        return RetVal.ok(page);
    }

    /*获取基本销售属性列表*/
    @GetMapping("queryAllSaleProperty")
    public RetVal queryAllSaleProperty() {
        List<BaseSaleProperty> list = salePropertyService.list(null);
        return RetVal.ok(list);
    }

    /*保存spu信息*/
    @PostMapping("saveProductSpu")
    public RetVal saveSpuInfo(@RequestBody ProductSpu productSpu) {
        spuService.saveInfo(productSpu);
        return RetVal.ok();
    }

    //获取销售属性列表
    @GetMapping("querySalePropertyByProductId/{productId}")
    public RetVal querySalePropertyByProductId(@PathVariable Long productId){
         List<ProductSalePropertyKey> list = salePropertyKeyService.querySalePropertyByProductId(productId);
        return RetVal.ok(list);
    }

    //获取图片集合
    @GetMapping("queryProductImageByProductId/{productId}")
    public RetVal queryProductImageByProductId(@PathVariable Long productId) {
        QueryWrapper<ProductImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId);
        List<ProductImage> list = imageService.list(queryWrapper);
        return RetVal.ok(list);
    }
}
